using System;
using System.IO;

//Aspose Namespaces
using Aspose.Pdf.Generator;
using Aspose.Words;
using Aspose.Cells;
using Aspose.Tasks;
using Aspose.Slides;
using Aspose.BarCode;
using Aspose.BarCodeRecognition;

namespace AsposeQuickStart
{
    public static class AsposeFileExpert
    {
        //create new document
        public static MemoryStream CreateNewDocument(DocumentType docType)
        {
            MemoryStream msTemp = new MemoryStream();
            switch (docType)
            {
                case DocumentType.PDF:
                    try
                    {
                        Console.WriteLine("Creating PDF document");
                        //Create pdf document
                        Aspose.Pdf.Generator.Pdf pdfDocument = new Aspose.Pdf.Generator.Pdf();
                        //Add a section into the pdf document
                        Aspose.Pdf.Generator.Section pdfSection = pdfDocument.Sections.Add();
                        //Add a text paragraph into the section
                        pdfSection.Paragraphs.Add(new Aspose.Pdf.Generator.Text("Aspose - Your File Format Experts!"));
                        //Save the document
                        pdfDocument.Save(msTemp);
                        Console.WriteLine("PDF document created successfully");
                    }
                    catch(Exception ex)
                    {
                        Settings.LogException(ex);
                        Console.WriteLine("Problem creating PDF document");
                    }
                    break;
                case DocumentType.DOC:
                    try
                    {
                        Console.WriteLine("Creating Word document");
                        //create document
                        Document wordsDocument = new Document();
                        //add text in the document
                        DocumentBuilder builder = new DocumentBuilder(wordsDocument);
                        builder.Writeln("Aspose - Your File Format Experts!");
                        wordsDocument.Save(msTemp, Aspose.Words.SaveFormat.Doc);
                        Console.WriteLine("Word document created successfully");
                    }
                    catch (Exception ex)
                    {
                        Settings.LogException(ex);
                        Console.WriteLine("Problem creating Word document");
                    }
                    break;
                case DocumentType.XLS:
                    try
                    {
                        Console.WriteLine("Creating Excel document");
                        //Instantiate a Workbook object that represents Excel file.
                        //create a workbook
                        Workbook CellsDocument = new Workbook();
                        //Note when you create a new workbook, a default worksheet
                        //"Sheet1" is added (by default) to the workbook.
                        //Access the first worksheet "Sheet1" in the book.
                        Worksheet sheet = CellsDocument.Worksheets[0];
                        //Access the "A1" cell in the sheet.
                        Aspose.Cells.Cell cell = sheet.Cells["A1"];
                        //Input text into the "A1" cell
                        cell.PutValue("Aspose - Your File Format Experts!");
                        //Save the Excel file.
                        CellsDocument.Save(msTemp, Aspose.Cells.SaveFormat.Excel97To2003);
                        Console.WriteLine("Excel document created successfully");
                    }
                    catch (Exception ex)
                    {
                        Settings.LogException(ex);
                        Console.WriteLine("Problem creating Excel document");
                    }
                    break;
                case DocumentType.PPT:
                    try
                    {
                        Console.WriteLine("Creating Powerpoint presentation");
                        //create presentation
                        Presentation presentation = new Presentation();
                        //add empty slide
                        Slide slide = presentation.AddEmptySlide();
                        //Adding a rectangle (X=2400, Y=1800, Width=1000 & Height=500) to the slide
                        Aspose.Slides.Rectangle rect = slide.Shapes.AddRectangle(2400, 1800, 1000, 500);
                        //Hiding the lines of rectangle
                        rect.LineFormat.ShowLines = false;
                        //Adding a text frame to the rectangle
                        rect.AddTextFrame("Aspose - Your File Format Experts!");
                        //Removing the first slide of the presentation which is always added by
                        //Aspose.Slides for .NET by default while creating the presentation
                        presentation.Slides.RemoveAt(0);
                        //save presentation
                        presentation.Write(msTemp);
                        Console.WriteLine("Powerpoint presentation created successfully");
                    }
                    catch (Exception ex)
                    {
                        Settings.LogException(ex);
                        Console.WriteLine("Problem creating Powerpoint presentation");
                    }
                    break;
                case DocumentType.BARCODE:
                    try
                    {
                        Console.WriteLine("Creating Barcode image");
                        //Instantiate BarCodeBuilder object
                        BarCodeBuilder barcode = new BarCodeBuilder();
                        //Set the Code text for the barcode
                        barcode.CodeText = "1234567";
                        //Set the symbology type to Code39Standard
                        barcode.SymbologyType = Symbology.Code39Standard;
                        //Set the width of the bars to 1 millimeter
                        barcode.xDimension = 1f;
                        //Save the barcode image
                        barcode.Save(msTemp, System.Drawing.Imaging.ImageFormat.Jpeg);
                        Console.WriteLine("Barcode image created successfully");
                    }
                    catch (Exception ex)
                    {
                        Settings.LogException(ex);
                        Console.WriteLine("Problem creating Barcode image");
                    }
                    break;
                case DocumentType.PROJECT:
                    try
                    {
                        Console.WriteLine("Creating Project file");
                        //create a prject instance
                        Project project = new Project();
                        //create a project writer instance
                        ProjectWriter prjWriter = new ProjectWriter();
                        //create project file
                        prjWriter.Write(project, msTemp, TasksDataFormat.XML);
                        Console.WriteLine("Project file created successfully");
                    }
                    catch (Exception ex)
                    {
                        Settings.LogException(ex);
                        Console.WriteLine("Problem creating Project file");
                    }
                    break;
                default:
                    break;
            }

            return msTemp;
        }

        //save document 
        public static bool SaveRetrievedDocument(DocumentType docType, string fileName, MemoryStream msDocument)
        {
            try
            {
                switch (docType)
                {
                    case DocumentType.PDF:
                        Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(msDocument);
                        pdfDocument.Save(fileName);
                        break;
                    case DocumentType.DOC:
                        Aspose.Words.Document wordsDocument = new Aspose.Words.Document(msDocument);
                        wordsDocument.Save(fileName, Aspose.Words.SaveFormat.Doc);
                        break;
                    case DocumentType.XLS:
                        Aspose.Cells.Workbook cellsDocument = new Aspose.Cells.Workbook(msDocument);
                        cellsDocument.Save(fileName, Aspose.Cells.SaveFormat.Excel97To2003);
                        break;
                    case DocumentType.PPT:
                        Aspose.Slides.Presentation presentation = new Aspose.Slides.Presentation(msDocument);
                        presentation.Save(fileName, Aspose.Slides.Export.SaveFormat.Ppt);
                        break;
                    case DocumentType.BARCODE:
                        Console.WriteLine("Recognizing Barcode...");
                        //recognize barcode
                        BarCodeReader reader = new BarCodeReader(msDocument, BarCodeReadType.Code39Standard);
                        while (reader.Read())
                        {
                            // get symbology type
                            Console.WriteLine("Symbology Type: " + reader.GetReadType());
                            // get code text
                            Console.WriteLine("CodeText: " + reader.GetCodeText());
                        }
                        reader.Close();
                        //save barcode image to disk
                        FileStream fsBarcode = new FileStream(fileName, FileMode.Create);
                        msDocument.WriteTo(fsBarcode);
                        fsBarcode.Close();
                        break;
                    case DocumentType.PROJECT:
                        //read existing project
                        ProjectReader projectReader = new ProjectReader();
                        Project project = projectReader.Read(msDocument);
                        //write project file to disk
                        ProjectWriter projectWriter = new ProjectWriter();
                        projectWriter.Write(project, fileName, TasksDataFormat.XML);
                        break;
                    default:
                        break;
                }

                return true;
            }
            catch(Exception ex)
            {
                Settings.LogException(ex);
                  return false;
            }
            
        }
    }
}
